using System;
using Castle.ActiveRecord;

namespace Scrummer.Models
{
	public abstract class AbstractModel<T> : ActiveRecordBase<T>
	{
		private int? id;

		[PrimaryKey]
		public int Id
		{
			get
			{
				if (!id.HasValue)
					throw new InvalidOperationException("Cannot access the ID of an object before it is created");

				return id.Value;
			}
			set { id = value; }
		}

		public bool IsNew
		{
			get { return !id.HasValue; }
		}

		public override void Save()
		{
			if (id == null)
				throw new InvalidOperationException("Cannot save a new object, call Create instead");
			else
			{
				OnSaving();
				base.Save();
				OnSaved();
			}
		}

		public override void Create()
		{
			OnCreating();
			base.Create();
			OnCreated();
		}

		protected virtual void OnSaving()
		{
			
		}

		protected virtual void OnSaved()
		{
			
		}

		protected virtual void OnCreating()
		{
		}

		protected virtual void OnCreated()
		{
		}
	}
}
